package o.mrs;

import org.json.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ListPersons extends Activity {
	private ListView lista;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listausr);
        	
        String obj;
		try {
	        obj = ApiRest.getRequestGet("person?q");
			JSONObject jsonObject = new JSONObject(obj);
	     	JSONArray arrayResult = (JSONArray) jsonObject.get("results");    
	     	String[] dispPerson = new  String[arrayResult.length()];
	     	final String[] uriPerson = new  String[arrayResult.length()];//<-----------------
	     	int largoArray = arrayResult.length();
	     	
	     	for(int contador=0; contador < largoArray; contador ++){
	     		JSONObject registro = (JSONObject) arrayResult.get(contador);
	     		String uuid = (String) registro.get("uuid");  	     				
	     		String display = (String) registro.get("display");  	     		
	     		dispPerson[contador] = display ; 
	     		uriPerson[contador] = uuid;
	     		
	     		
	     	/*	JSONArray arrayResultLinks = (JSONArray)  registro.get("links");
	     		int largoArrayLinks = arrayResultLinks.length();	         	
	        	for(int contadorLinks=0; contadorLinks < largoArrayLinks; contadorLinks ++){
	        		JSONObject registroLink = (JSONObject) arrayResultLinks.get(contadorLinks);
	         		String uri = (String) registroLink.get("uri");	   
	         		uriPerson[contador] = uri ;
    			    }	   */
	        	
	        	
	     	}
	     	 lista=(ListView) findViewById(R.id.listPer); 

	         ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dispPerson); 
	         lista.setAdapter(adapter);//carga adaptador a la lista  
	     	 
	         lista.setOnItemClickListener(new OnItemClickListener() {
	         	 
					public void onItemClick(AdapterView<?> parent, View view, int posicion, long id) {
						//Toast.makeText(getApplicationContext(),uriPerson[posicion], Toast.LENGTH_SHORT).show();	
						Intent i = new Intent();
						i.putExtra("URI",uriPerson[posicion]);
						i.setClass(getApplicationContext(),Person.class);	
						startActivity(i);
					}
	         });	
	         
	         
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
     	
	}
	

}
